<?php

namespace addons\rexam\controller;

class Index extends Base
{
    public function index()
    {
        //群组
        $group_model = new \app\admin\model\rexam\Group;
        $group_ids = $group_model->idchain($this->rexam_user['rexam_group_id']);
        $group_ids[] = 0;

        //考试
        $current_time = time();
        $rexam_model = new \app\admin\model\rexam\Rexam;
        $where = array();
        $where['status']='normal';
        $where['expiretime']=array('gt',$current_time);
//        $where['rexam_group_id']=array('in', $group_ids);

        $answer_model = new \app\admin\model\rexam\Answer;

        //groupdata:考生组验证
        $list = $rexam_model
            ->where($where)
            ->order('starttime asc')
            ->select();

        //考试状态
        foreach ($list as $k=>$v){
            if($v['starttime']>=$current_time){
                $list[$k]['rexam_status'] = 0;//未开始
            }else{
                $list[$k]['rexam_status'] = 1;//已开始
                $answer = $answer_model->where(['user_id'=>$this->auth->id, 'rexam_id'=>$v['id']])->find();
                if($answer){
                    if($answer['overtime']){
                        $list[$k]['rexam_status'] = 3;//已完成
                    }else{
                        $list[$k]['rexam_status'] = 2;//答题中
                    }
                }
            }
        }

        $this->assign('list', $list);

        return $this->view->fetch();
    }

    public function ranking(){
        return $this->view->fetch();
    }

    public function me(){


        $user_model = new \app\admin\model\rexam\User;

        $table = $user_model->getTable();
        $me = $user_model
            ->where([$table.'.id'=>$this->rexam_user['id']])
            ->with(['sysuser','rexamgroup'])
            ->find();
        $this->assign('me', $me);

        $answer_model = new \app\admin\model\rexam\Answer;
        $answer_list = $answer_model
            ->with(['rexam'])
            ->where(['user_id'=>$this->auth->id])
            ->order('overtime desc')
            ->select();
        $this->assign('answer_list', $answer_list);
        
        return $this->view->fetch();
    }
}
